/**
 * 初始化专家介绍对话框
 */
var ExpertInfoDlg = {
    expertInfoData: {},
    editor: null,
    validateFields: {
    	name: {
            validators: {
                notEmpty: {
                    message: '专家名称不能为空'
                }
            }
        },
        pri: {
            validators: {
                notEmpty: {
                    message: '排序不能为空'
                }
            }
        },
        rank: {
            validators: {
                notEmpty: {
                    message: '职称不能为空'
                }
            }
        },
        picUrl: {
            validators: {
                notEmpty: {
                    message: '友链图片不能为空'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
ExpertInfoDlg.clearData = function () {
    this.expertInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ExpertInfoDlg.set = function (key, value) {
    this.expertInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ExpertInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
ExpertInfoDlg.close = function () {
    parent.layer.close(window.parent.ExpertInfo.layerIndex);
}

/**
 * 收集数据
 */
ExpertInfoDlg.collectData = function () {
	this.expertInfoData['content'] = ExpertInfoDlg.editor.txt.html();
    this.set('id').set('name').set('pri').set('rank').set('picUrl');
}

/**
 * 收集更新数据
 */
ExpertInfoDlg.collectUpdateData = function () {
	this.expertInfoData['content'] = ExpertInfoDlg.editor.txt.html();
    this.set('id').set('name').set('pri').set('rank').set('picUrl');
}

/**
 * 验证数据是否为空
 */
ExpertInfoDlg.validate = function () {
    $('#expertInfoForm').data("bootstrapValidator").resetForm();
    $('#expertInfoForm').bootstrapValidator('validate');
    return $("#expertInfoForm").data('bootstrapValidator').isValid();
};

/**
 * 提交添加
 */
ExpertInfoDlg.addSubmit = function () {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }
    if(this.get('picUrl') == ''){
		Feng.error("图片不能为空");
		return;
	}
    if (isNaN(this.expertInfoData.pri)) {
        Feng.error('排序值请输入数字！');
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/hp/expert/add", function (data) {
        Feng.success("添加成功!");
        window.parent.ExpertInfo.table.refresh();
        ExpertInfoDlg.close();
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.expertInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
ExpertInfoDlg.editSubmit = function () {

    this.clearData();
    this.collectUpdateData();

    if (!this.validate()) {
        return;
    }
    if(this.get('picUrl') == ''){
		Feng.error("图片不能为空");
		return;
	}
    if (isNaN(this.expertInfoData.pri)) {
        Feng.error('排序值请输入数字！');
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/hp/expert/update", function (data) {
        Feng.success("修改成功!");
        window.parent.ExpertInfo.table.refresh();
        ExpertInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.expertInfoData);
    ajax.start();
}

$(function () {
    Feng.initValidator("expertInfoForm", ExpertInfoDlg.validateFields);

    // 初始化头像上传
    var avatarUp = new $WebUpload("picUrl");
    avatarUp.setUploadBarId("progressBar");
    avatarUp.init();
    
    //初始化编辑器
    var E = window.wangEditor;
    var editor = new E('#editor');
    //editor.customConfig.uploadImgShowBase64 = true;
    editor.customConfig.uploadImgServer = '/hp/pic/wangeditorUpload';
    editor.customConfig.uploadFileName = 'picture';
    editor.create();
    editor.txt.html($("#contentVal").val());
    ExpertInfoDlg.editor = editor;
});
